//
//  CategoryFilterView.m
//  quanminzhekou
//
//  Created by zxl on 15/1/13.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CategoryFilterView.h"

@interface CategoryFilterView ()
@property (nonatomic) UIButton* allBtn;
@property (nonatomic) UIButton* taobaoBtn;
@property (nonatomic) UIButton* tmallBtn;

@property (nonatomic) UIView* priceSpaceBg;
@property (nonatomic) UIImageView* minImg;
@property (nonatomic) UIImageView* maxImg;
@property (nonatomic) UIView* priceSpace;

@property (nonatomic) CGPoint beginPoint;

@property (nonatomic) NSString* sourceStr;
@property (nonatomic) NSString* minPrice;
@property (nonatomic) NSString* maxPrice;

@property (nonatomic) NSMutableArray* labsArr;
@end

@implementation CategoryFilterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 2.0;
        self.layer.borderWidth = 1.0;
        self.layer.borderColor = [UIColor colorWithRed:0xe2 / 255.0 green:0xe3 / 255.0 blue:0xe3 / 255.0 alpha:1.0].CGColor;
        
        UILabel* goodsTypeLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 12, 60, 11)];
        goodsTypeLab.backgroundColor = [UIColor clearColor];
        goodsTypeLab.text = @"商品类型:";
        goodsTypeLab.font = [UIFont systemFontOfSize:10];
        goodsTypeLab.textColor = [UIColor lightGrayColor];
        [self addSubview:goodsTypeLab];
        
        CGRect btnFrame = CGRectMake(CGRectGetMinX(goodsTypeLab.frame), CGRectGetMaxY(goodsTypeLab.frame) + 11, 71 / 2.0, 71 / 2.0);
        
        _allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _allBtn.frame = btnFrame;
        _allBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_allBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_allBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [_allBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_allBtn setTitle:@"全部" forState:UIControlStateNormal];
        [_allBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_normal"] forState:UIControlStateNormal];
        [_allBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_select"] forState:UIControlStateHighlighted];
        [_allBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_select"] forState:UIControlStateSelected];
        [_allBtn addTarget:self action:@selector(onTapAllBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_allBtn];
        
        _taobaoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _taobaoBtn.frame = CGRectOffset(btnFrame, (71 / 2.0 + 10) * 1, 0);
        _taobaoBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_taobaoBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_taobaoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [_taobaoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_taobaoBtn setTitle:@"淘宝" forState:UIControlStateNormal];
        [_taobaoBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_normal"] forState:UIControlStateNormal];
        [_taobaoBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_select"] forState:UIControlStateHighlighted];
        [_taobaoBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_select"] forState:UIControlStateSelected];
        [_taobaoBtn addTarget:self action:@selector(onTapTaobaoBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_taobaoBtn];
        
        _tmallBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _tmallBtn.frame = CGRectOffset(btnFrame, (71 / 2.0 + 10) * 2, 0);
        _tmallBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_tmallBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_tmallBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [_tmallBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_tmallBtn setTitle:@"天猫" forState:UIControlStateNormal];
        [_tmallBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_normal"] forState:UIControlStateNormal];
        [_tmallBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_select"] forState:UIControlStateHighlighted];
        [_tmallBtn setBackgroundImage:[UIImage imageNamed:@"filter_button_select"] forState:UIControlStateSelected];
        [_tmallBtn addTarget:self action:@selector(onTapTmallBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_tmallBtn];
        
        UILabel* priceSpaceLab = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_allBtn.frame) + 20, 60, 11)];
        priceSpaceLab.backgroundColor = [UIColor clearColor];
        priceSpaceLab.text = @"价格区间:";
        priceSpaceLab.font = [UIFont systemFontOfSize:10];
        priceSpaceLab.textColor = [UIColor lightGrayColor];
        [self addSubview:priceSpaceLab];
        
        _priceSpaceBg = [[UIView alloc] initWithFrame:CGRectMake(13, CGRectGetMaxY(priceSpaceLab.frame) + 19, CGRectGetWidth(frame) - 20 - 13, 5)];
        _priceSpaceBg.backgroundColor = [UIColor lightGrayColor];
        _priceSpaceBg.layer.cornerRadius = 2.0;
        _priceSpaceBg.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self addSubview:_priceSpaceBg];
        
        _minImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"filter_price"]];
        _minImg.frame = CGRectMake(0, 0, 71 / 2.0, 71 / 2.0);
        _minImg.center = CGPointMake(CGRectGetMinX(_priceSpaceBg.frame), CGRectGetMidY(_priceSpaceBg.frame));
        _minImg.userInteractionEnabled = YES;
        [_minImg addObserver:self forKeyPath:@"center" options:0 context:nil];
        [self addSubview:_minImg];
        
        UIPanGestureRecognizer* pan_min = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMinImg:)];
        [_minImg addGestureRecognizer:pan_min];
        
        _maxImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"filter_price"]];
        _maxImg.frame = CGRectMake(0, 0, 71 / 2.0, 71 / 2.0);
        _maxImg.center = CGPointMake(CGRectGetMaxX(_priceSpaceBg.frame), CGRectGetMidY(_priceSpaceBg.frame));
        _maxImg.userInteractionEnabled = YES;
        [_maxImg addObserver:self forKeyPath:@"center" options:0 context:nil];
        [self addSubview:_maxImg];
        
        UIPanGestureRecognizer* pan_max = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMaxImg:)];
        [_maxImg addGestureRecognizer:pan_max];
        
        _priceSpace = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(_minImg.frame), CGRectGetMinY(_priceSpaceBg.frame), CGRectGetMidX(_maxImg.frame) - CGRectGetMidX(_minImg.frame), CGRectGetHeight(_priceSpaceBg.frame))];
        _priceSpace.backgroundColor = [UIColor redColor];
        _priceSpace.layer.cornerRadius = 2.0;
        _priceSpace.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self insertSubview:_priceSpace aboveSubview:_priceSpaceBg];
        
        
        UIImageView* priceRuler = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"filter_price_ruler"]];
        priceRuler.frame = CGRectMake(13, CGRectGetMaxY(_priceSpaceBg.frame) + 13, CGRectGetWidth(frame) - 13 - 20, 7 / 2.0);
        priceRuler.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self addSubview:priceRuler];
        
        _labsArr = [[NSMutableArray alloc] init];
        
        for (NSInteger index = 0; index < 6; ++index) {
            UILabel* priceLab = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(priceRuler.frame) + 2, 25, 12)];
            priceLab.center = CGPointMake(CGRectGetWidth(priceRuler.frame) / 5 * index + CGRectGetMinX(priceRuler.frame), priceLab.center.y);
            priceLab.backgroundColor = [UIColor clearColor];
            priceLab.textColor = [UIColor lightGrayColor];
            priceLab.textAlignment = NSTextAlignmentCenter;
            priceLab.font = [UIFont systemFontOfSize:8];
            [self addSubview:priceLab];
            [_labsArr addObject:priceLab];
            
            NSString* text = @"";
            switch (index) {
                case 0:
                    text = @"￥0";
                    break;
                case 1:
                    text = @"￥10";
                    break;
                case 2:
                    text = @"￥20";
                    break;
                case 3:
                    text = @"￥50";
                    break;
                case 4:
                    text = @"￥100";
                    break;
                default:
                    text = @"不限";
                    break;
            }
            priceLab.text = text;
        }
    }
    
    return self;
}

- (void)dealloc
{
    [self.minImg removeObserver:self forKeyPath:@"center"];
    [self.maxImg removeObserver:self forKeyPath:@"center"];
}

- (void)layoutCategoryFilterViewSubviews {
    self.minImg.center = CGPointMake(CGRectGetMinX(_priceSpaceBg.frame), CGRectGetMidY(_priceSpaceBg.frame));
    self.maxImg.center = CGPointMake(CGRectGetMaxX(_priceSpaceBg.frame), CGRectGetMidY(_priceSpaceBg.frame));
    
    for (NSInteger index = 0; index < self.labsArr.count; ++index) {
        UILabel* lab = [self.labsArr objectAtIndex:index];
        lab.center = CGPointMake(CGRectGetWidth(self.priceSpaceBg.frame) / 5 * index + CGRectGetMinX(self.priceSpaceBg.frame), lab.center.y);
    }
}

- (void)setupCategoryFileterDefulte {
    _minImg.center = CGPointMake(CGRectGetMinX(_priceSpaceBg.frame), CGRectGetMidY(_priceSpaceBg.frame));
    _maxImg.center = CGPointMake(CGRectGetMaxX(_priceSpaceBg.frame), CGRectGetMidY(_priceSpaceBg.frame));
    _minPrice = @"0";
    _maxPrice = @"all";
    
    [self setupBtnSelected:self.allBtn];
    self.sourceStr = @"all";
}

- (NSString*)filterString {
    _filterString = [NSString stringWithFormat:@"%@_%@-%@", self.sourceStr, self.minPrice, self.maxPrice];
    
    if ([_filterString isEqualToString:@"all_0-all"]) {
        return nil;
    }
    
    return _filterString;
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"center"]) {
        CGRect frame = self.priceSpace.frame;
        frame.origin.x = CGRectGetMidX(_minImg.frame);
        frame.size.width = CGRectGetMidX(_maxImg.frame) - CGRectGetMidX(_minImg.frame);
        self.priceSpace.frame = frame;
    }
}

#pragma mark -
- (void)onTapAllBtn:(UIButton*)btn {
    self.sourceStr = @"all";
    [self setupBtnSelected:btn];
}

- (void)onTapTaobaoBtn:(UIButton*)btn {
    self.sourceStr = @"taobao";
    [self setupBtnSelected:btn];
}

- (void)onTapTmallBtn:(UIButton*)btn {
    self.sourceStr = @"tmall";
    [self setupBtnSelected:btn];
}

- (void)setupBtnSelected:(UIButton*)btn {
    self.allBtn.selected = NO;
    self.taobaoBtn.selected = NO;
    self.tmallBtn.selected = NO;
    
    btn.selected = YES;
}

- (void)panMinImg:(UIPanGestureRecognizer*)pan {
    if (UIGestureRecognizerStateBegan == pan.state) {
        _beginPoint = [pan translationInView:self];
    } else if (UIGestureRecognizerStateChanged == pan.state) {
        CGPoint point = [pan translationInView:self];
        CGPoint changedPoint = CGPointMake(point.x - _beginPoint.x, point.y - _beginPoint.y);
        _beginPoint = point;
        
        CGFloat minCenterX = _minImg.center.x + changedPoint.x;
        CGFloat maxCenterX = _maxImg.center.x;
        
        if (minCenterX <= CGRectGetMinX(self.priceSpaceBg.frame)) {
            minCenterX = CGRectGetMinX(self.priceSpaceBg.frame);
        } else if (minCenterX >= (maxCenterX - CGRectGetMaxX(self.priceSpaceBg.frame) / 5)) {
            minCenterX = (maxCenterX - CGRectGetMaxX(self.priceSpaceBg.frame) / 5);
        }
        _minImg.center = CGPointMake(minCenterX, _minImg.center.y);
    } else if (UIGestureRecognizerStateEnded == pan.state) {
        CGFloat offset = CGRectGetWidth(self.priceSpaceBg.frame) / 5.0;
        CGFloat currentPos = _minImg.center.x - CGRectGetMinX(self.priceSpaceBg.frame);
        NSInteger num_integer = currentPos / offset;
        CGFloat up_offset = offset * (num_integer + 1) - currentPos;
        CGFloat down_offset = offset * num_integer - currentPos;
        if (fabs(up_offset) >= fabs(down_offset)) {
            _minImg.center = CGPointMake(offset * num_integer + CGRectGetMinX(self.priceSpaceBg.frame), _minImg.center.y);
        } else {
            _minImg.center = CGPointMake(offset * (num_integer + 1) + CGRectGetMinX(self.priceSpaceBg.frame), _minImg.center.y);
        }
        
        _minPrice = [self getPriceStrWithCenterPoint:_minImg.center];
    }
}

- (void)panMaxImg:(UIPanGestureRecognizer*)pan {
    if (UIGestureRecognizerStateBegan == pan.state) {
        _beginPoint = [pan translationInView:self];
    } else if (UIGestureRecognizerStateChanged == pan.state) {
        CGPoint point = [pan translationInView:self];
        CGPoint changedPoint = CGPointMake(point.x - _beginPoint.x, point.y - _beginPoint.y);
        _beginPoint = point;
        
        CGFloat minCenterX = _minImg.center.x;
        CGFloat maxCenterX = _maxImg.center.x + changedPoint.x;
        
        if (maxCenterX >= CGRectGetMaxX(self.priceSpaceBg.frame)) {
            maxCenterX = CGRectGetMaxX(self.priceSpaceBg.frame);
        } else if (maxCenterX <= (minCenterX + CGRectGetMaxX(self.priceSpaceBg.frame) / 5)) {
            maxCenterX = (minCenterX + CGRectGetMaxX(self.priceSpaceBg.frame) / 5);
        }
        _maxImg.center = CGPointMake(maxCenterX, _minImg.center.y);
    } else if (UIGestureRecognizerStateEnded == pan.state) {
        CGFloat offset = CGRectGetWidth(self.priceSpaceBg.frame) / 5.0;
        CGFloat currentPos = _maxImg.center.x - CGRectGetMinX(self.priceSpaceBg.frame);
        NSInteger num_integer = currentPos / offset;
        CGFloat up_offset = offset * (num_integer + 1) - currentPos;
        CGFloat down_offset = offset * num_integer - currentPos;
        if (fabs(up_offset) >= fabs(down_offset)) {
            _maxImg.center = CGPointMake(offset * num_integer + CGRectGetMinX(self.priceSpaceBg.frame), _maxImg.center.y);
        } else {
            _maxImg.center = CGPointMake(offset * (num_integer + 1) + CGRectGetMinX(self.priceSpaceBg.frame), _maxImg.center.y);
        }
        
        _maxPrice = [self getPriceStrWithCenterPoint:_maxImg.center];
    }
}

- (NSString*)getPriceStrWithCenterPoint:(CGPoint)center {
    NSString* str = @"";
    CGFloat offset = CGRectGetWidth(self.priceSpaceBg.frame) / 5.0;
    NSInteger index = (center.x - 13) / offset;
    switch (index) {
        case 0:
            str = @"0";
            break;
        case 1:
            str = @"10";
            break;
        case 2:
            str = @"20";
            break;
        case 3:
            str = @"50";
            break;
        case 4:
            str = @"100";
            break;
        default:
            str = @"all";
            break;
    }
    
    return str;
}

@end
